﻿<%@ WebHandler Language="C#" Class="xinxi_peibidan" %>

using System;
using System.Web;
using KeaoBase;
using System.Configuration;
using System.Data;
using System.Collections.Generic;

public class xinxi_peibidan : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["requestType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                if (requestType == "1")
                {
                    string leiXing = string.Format("{0}", context.Request.Form["leixing"]);
                    string zhanDian = string.Format("{0}", context.Request.Form["zhandian"]);
                    string createCondition = string.Empty;
                    if (!string.IsNullOrEmpty(leiXing) && leiXing != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("leixing", DataValueCommonType.字符串, leiXing, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(zhanDian) && zhanDian != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                    }
                    Dictionary<string, string> orderBys = new Dictionary<string, string>();
                    orderBys.Add("mingcheng", "asc");
                        orderBys.Add("peibiwuliao","asc");
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_peibidan", createCondition,orderBys);
                    string responseJson = KeaoUtils.Dt2Json(Dt);
                    context.Response.Write(responseJson);
                }
                if (requestType == "2")
                {
                    //编辑信息
                    string peibidanid = string.Format("{0}", context.Request.Form["peibidanid"]);
                    string mingcheng = string.Format("{0}", context.Request.Form["mingcheng"]);
                    string peibi = string.Format("{0}", context.Request.Form["peibi"]);
                    string peibiwuliao = string.Format("{0}", context.Request.Form["peibiwuliao"]);
                    string guanlianbianhao = string.Format("{0}", context.Request.Form["guanlianbianhao"]);
                    string zhandian = string.Format("{0}", context.Request.Form["zhandian"]);
                    string leixing = string.Format("{0}", context.Request.Form["leixing"]);
                    if (string.IsNullOrEmpty(peibidanid))
                    {
                        //新增
                        TableRow newRow = new TableRow("tbl_peibidan", Guid.NewGuid().ToString(), DBCtl.插入);
                        TableCol col1 = new TableCol();
                        col1.ColName = "mingcheng";
                        col1.ValueType = DataValueCommonType.字符串;
                        col1.ValueMember = mingcheng;
                        newRow.DataCols.Add(col1);
                        TableCol col2 = new TableCol();
                        col2.ColName = "peibi";
                        col2.ValueType = DataValueCommonType.字符串;
                        col2.ValueMember = peibi;
                        newRow.DataCols.Add(col2);
                        TableCol col3 = new TableCol();
                        col3.ColName = "guanlianbianhao";
                        col3.ValueMember = guanlianbianhao;
                        col3.ValueType = DataValueCommonType.字符串;
                        newRow.DataCols.Add(col3);
                        TableCol col4 = new TableCol();
                        col4.ColName = "peibiwuliao";
                        col4.ValueType = DataValueCommonType.字符串;
                        col4.ValueMember = peibiwuliao.Contains("(") ? peibiwuliao.Substring(0,peibiwuliao.LastIndexOf("(")) : peibiwuliao;
                        newRow.DataCols.Add(col4);
                        TableCol col5 = new TableCol();
                        col5.ColName = "shangchuanzhuangtai";
                        col5.ValueMember = "1";
                        col5.ValueType = DataValueCommonType.字符串;
                        newRow.DataCols.Add(col5);
                        TableCol col6 = new TableCol();
                        col6.ColName = "zhandian";
                        col6.ValueMember = zhandian;
                        col6.ValueType = DataValueCommonType.字符串;
                        newRow.DataCols.Add(col6);
                        TableCol col7 = new TableCol();
                        col7.ColName = "kehubianhao";
                        col7.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                        col7.ValueType = DataValueCommonType.字符串;
                        newRow.DataCols.Add(col7);
                        TableCol col8 = new TableCol();
                        col8.ColName = "guanlianwuliao";
                        col8.ValueMember = peibiwuliao;
                        col8.ValueType = DataValueCommonType.字符串;
                        newRow.DataCols.Add(col8);
                        TableCol col9 = new TableCol();
                        col9.ColName = "leixing";
                        col9.ValueMember = leixing;
                        col9.ValueType = DataValueCommonType.字符串;
                        newRow.DataCols.Add(col9);
                        if (m_DB.SaveTableRows(newRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                    }
                    else
                    {
                        //修改
                        TableRow editRow = new TableRow("tbl_peibidan", peibidanid, DBCtl.修改);
                        TableCol col1 = new TableCol();
                        col1.ColName = "mingcheng";
                        col1.ValueType = DataValueCommonType.字符串;
                        col1.ValueMember = mingcheng;
                        editRow.DataCols.Add(col1);
                        TableCol col2 = new TableCol();
                        col2.ColName = "peibi";
                        col2.ValueType = DataValueCommonType.字符串;
                        col2.ValueMember = peibi;
                        editRow.DataCols.Add(col2);
                        TableCol col3 = new TableCol();
                        col3.ColName = "guanlianbianhao";
                        col3.ValueMember = guanlianbianhao;
                        col3.ValueType = DataValueCommonType.字符串;
                        editRow.DataCols.Add(col3);
                        TableCol col4 = new TableCol();
                        col4.ColName = "peibiwuliao";
                        col4.ValueType = DataValueCommonType.字符串;
                        col4.ValueMember = peibiwuliao.Contains("(") ? peibiwuliao.Substring(0,peibiwuliao.LastIndexOf("(")) : peibiwuliao;
                        editRow.DataCols.Add(col4);
                        TableCol col6 = new TableCol();
                        col6.ColName = "zhandian";
                        col6.ValueMember = zhandian;
                        col6.ValueType = DataValueCommonType.字符串;
                        editRow.DataCols.Add(col6);
                        TableCol col8 = new TableCol();
                        col8.ColName = "guanlianwuliao";
                        col8.ValueMember = peibiwuliao;
                        col8.ValueType = DataValueCommonType.字符串;
                        editRow.DataCols.Add(col8);
                        if (m_DB.SaveTableRows(editRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                    }
                }
                if (requestType == "3")
                {
                    //删除信息
                    string peibidanid = string.Format("{0}", context.Request.Form["peibidanid"]);
                    if (string.IsNullOrEmpty(peibidanid))
                    {
                        context.Response.Write("没有找到信息，请重试。");
                        return;
                    }
                    TableRow delRow = new TableRow();
                    delRow.ID = peibidanid;
                    delRow.TableName = "tbl_peibidan";
                    if (m_DB.DeleteRowByID(delRow) > 0)
                    {
                        context.Response.Write("OK");
                    }
                    else
                    {
                        context.Response.Write("Failed");
                    }

                }
                if (requestType == "4")
                {
                    string leiXing = string.Format("{0}", context.Request.Form["leixing"]);
                    string zhanDian = string.Format("{0}", context.Request.Form["zhandian"]);
                    string createCondition = string.Empty;
                    if (!string.IsNullOrEmpty(leiXing) && leiXing != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("leixing", DataValueCommonType.字符串, leiXing, "", SearchConditionType.等于);
                    }
                    if (!string.IsNullOrEmpty(zhanDian) && zhanDian != "—全部—")
                    {
                        createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                    }
                    DataTable Dt = m_DB.GetDataTableByCondition("tbl_peibidan", "mingcheng", null, createCondition, "mingcheng", null);
                    string responseJson = KeaoUtils.Dt2Json(Dt);
                    context.Response.Write(responseJson);

                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("配比单信息异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}